<template>
  <div>
    <el-dialog
      :title="title"
      :visible="dialogFormVisible"
      @close="closeDialog"
      @open="openDialog"
    >
      <div v-if="isPreview">
        <h2 class="title">{{ form.title }}</h2>
        <div class="detail">
          <span>{{ form.createTime | formatTime('YYYY-MM-DD hh:mm:ss') }}</span>
          <span>{{ form.username }}</span>
          <i class="el-icon-view" />
          <span>{{ form.visits }}</span>
          <div class="context" v-html="form.articleBody" />
        </div>
      </div>
      <el-form
        v-else
        ref="addArticle"
        :model="form"
        label-width="100px"
        :rules="rules"
        v-loading="loading"
      >
        <el-form-item label="文章标题" prop="title">
          <el-input v-model="form.title" />
        </el-form-item>
        <el-form-item label="文章内容" prop="articleBody">
          <editor v-model="form.articleBody" :min-height="192" />
        </el-form-item>
        <el-form-item label="视频地址">
          <el-input v-model="form.videoURL" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer" v-if="!isPreview">
        <el-button @click="closeDialog">取 消</el-button>
        <el-button type="primary" @click="submitAdduser">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  addAritcleListApi,
  getAritcleDetailApi,
  editAritcleDetailApi
} from '@/api/articles'
import { getPermissonSimpleListApi } from '@/api/permisson'
export default {
  props: {
    dialogFormVisible: Boolean
  },
  data() {
    return {
      title: '',
      loading: false,
      form: {
        title: '',
        articleBody: '',
        videoURL: 'http://v.itheima.com/zycbrm2.mp4'
      },
      isPreview: false,
      rules: {
        title: [
          {
            required: true,
            message: '文章标题不能为空',
            trigger: ['blur', 'change']
          }
        ],
        articleBody: [
          {
            required: true,
            message: '文章内容不能为空',
            trigger: ['blur', 'change']
          }
        ]
      }
    }
  },
  computed: {
    /*   title() {
      return this.form.id ? '编辑文章' : '新建文章'
    } */
  },
  methods: {
    closeDialog() {
      this.$emit('update:dialogFormVisible', false)
      this.form = {
        title: '',
        articleBody: '',
        videoURL: 'http://v.itheima.com/zycbrm2.mp4'
      }
      this.isPreview = false
    },
    async openDialog(val, id) {
      if (val === 'add') this.title = '新增文章'
      if (val === 'edit') this.title = '编辑文章'
      if (val === 'preview') this.title = '预览文章'
      if (id) {
        const { data: data } = await getAritcleDetailApi(id)
        this.form = data
        this.loading = true
        if (val === 'preview') {
          const { data: data } = await getPermissonSimpleListApi()
          data.forEach((item) => {
            if (item.id === this.form.creatorID) {
              this.form.username = item.title
            }
          })
          this.isPreview = true
        }
      }

      this.loading = false
    },
    async submitAdduser() {
      this.$refs.addArticle.validate(async (flag) => {
        if (!flag) return
        if (this.form.id) {
          await editAritcleDetailApi(this.form.id, this.form)
          this.$message.success('编辑成功')
        } else {
          await addAritcleListApi(this.form)
          this.$message.success('添加成功')
        }
        this.closeDialog()
        this.$emit('updateArticle')
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.dialog-footer {
  display: flex;
  justify-content: flex-end;
}
.title {
  margin: 0;
  padding: 0;
  margin-bottom: 10px;
}
.detail {
  span {
    margin-left: 10px;
  }
}
.context {
  margin-top: 10px;
  background: #f5f5f5;
  padding: 10px;
  border-top: 1px dashed #ccc;
}
</style>
